package com.right.health.app.ui.me;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.TimeUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.ReviewReminderInfo;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.RemindApi;

/*
 * Created by hopeliao on 2019-09-29
 *
 */
public class ReviewReminderDetailActivity extends BaseActivity implements OnHttpListener {

    private static final int HTTP_CODE_DEL = 1;
    private static final int HTTP_CODE_UPDATE_REMIND = 2;

    private ReviewReminderInfo mResponse;

    private TextView mNameTv, mNextTimeTv, mEndTimeTv;
    private TextView mCycleTv, mTipsTimeTv, mDescTv;

    private SwitchCompat mRemindCompat;

    private RemindApi mRemindApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.review_reminder_detail);

    }

    @Override
    public void findView() {
        mRemindCompat = (SwitchCompat) findViewById(R.id.switchcompat_remind);
        mNameTv = (TextView) findViewById(R.id.tv_name);
        mNextTimeTv = (TextView) findViewById(R.id.tv_next_time);
        mEndTimeTv = (TextView) findViewById(R.id.tv_end_time);
        mCycleTv = (TextView) findViewById(R.id.tv_cycle);
        mTipsTimeTv = (TextView) findViewById(R.id.tv_tips_time);
        mDescTv = (TextView) findViewById(R.id.tv_desc);

        findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog dialog = DataCommon.getDeleteDialog(ReviewReminderDetailActivity.this, (dialog1, which) -> {
                    dialog1.dismiss();
                    if (mResponse != null) {
                        mRemindApi.addRequestCode(HTTP_CODE_DEL);
                        mRemindApi.deleteReviewRemind(mResponse.visitagainid);
                    }
                });
                dialog.show();
            }
        });
    }

    @Override
    public void initialize() {
        mRemindApi = new RemindApi();
        mRemindApi.setListener(this);

        Bundle bundle = getIntent().getExtras();
        if(bundle != null && bundle.containsKey(BroadCastKey.DATA)) {
            mResponse = bundle.getParcelable(BroadCastKey.DATA);
            refreshUI();
        }
    }

    private void refreshUI() {
        if(mResponse != null) {
            try {
                mNameTv.setText(mResponse.visitagainname);
                mDescTv.setText(mResponse.visitdesc);

                String nextTime = TimeUtil.convertTime(AppConfig.FORMAT_TIME, Long.parseLong(mResponse.nextvisittime));
                mNextTimeTv.setText(nextTime);

                String endTime = TimeUtil.convertTime(AppConfig.FORMAT_TIME, Long.parseLong(mResponse.visitendtime));
                mEndTimeTv.setText(endTime);

                String[] cycles = getResources().getStringArray(R.array.review_reminder_cycle_arrays);
                mCycleTv.setText(cycles[Integer.valueOf(mResponse.visittype)]);

                String[] tipsTime = getResources().getStringArray(R.array.review_reminder_tips_arrays);
                mTipsTimeTv.setText(cycles[Integer.valueOf(mResponse.remindtype)]);

                boolean checked = false;
                if ("1".equals(mResponse.remindstatus)) {
                    checked = true;
                }
                mRemindCompat.setChecked(checked);
                mRemindCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    if(mResponse != null) {
                        String target = isChecked ? "1" : "0";

                        mRemindApi.setTag(target);
                        mRemindApi.addRequestCode(HTTP_CODE_UPDATE_REMIND);
                        mRemindApi.modifyReviewRemind(mResponse.visitagainid, target);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_DEL) {
                ToastUtil.show(this, "删除成功");
                sendBroadcast(BroadCastCode.RREFRESH_DRUGREINDE);
                finish();
            } else if(request.requestCode == HTTP_CODE_UPDATE_REMIND) {
                String target = String.valueOf(request.tag);
                if(mResponse != null) {
                    mResponse.remindstatus = target;
                }
                ToastUtil.show(this, "修改成功");
                sendBroadcast(BroadCastCode.RREFRESH_DRUGREINDE);
            }

        }
    }
}
